package demo4.mapper;

import demo4.pojo.User;
import demo4.util.IDBUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@Component
public class UserMapper implements IUserMapper {
    @Autowired
    private IDBUtil dbUtil;

    @Override
    public int insert(User user) {
        try (Connection conn = dbUtil.getConnection();) {
            String sql = "INSERT INTO user(username,password,nickname,age) " +
                    "VALUES (?,?,?,?) ";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getPassword());
            ps.setString(3, user.getNickname());
            ps.setInt(4, user.getAge());
            return ps.executeUpdate();


        } catch (SQLException e) {
            e.printStackTrace();
        }

        return 0;
    }// 注册方法

    /*
    public boolean logIn(User user) {
        try(Connection conn = dbUtil.getConnection();){
            String sql = "SELECT username,password,nickname,age " +
                    "FROM user " +
                    "WHERE username = ? AND password = ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,user.getUsername());
            ps.setString(2,user.getPassword());
            ResultSet rs = ps.executeQuery();
            return rs.next();

        }catch (SQLException e){
            e.printStackTrace();
        }
        return false;}
     */ // 登录方法(自己写的)

    public User selectByUsername(String username) {
        try (Connection conn = dbUtil.getConnection()) {
            String sql = "SELECT username,password,nickname,age " +
                    "FROM user " +
                    "WHERE username = ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, username);
            ResultSet rs = ps.executeQuery();

            if (rs.next()) {
                User user = new User();
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                user.setAge(rs.getInt("age"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }// 登录方法(老师写的)

    /*
    public List<String> query(){
        try(Connection conn = dbUtil.getConnection()){
            String sql = "SELECT nickname " +
                    "FROM user ";
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery(sql);
            List<String> nicknames = new ArrayList<>();
            while (rs.next()){
                nicknames.add(rs.getString("nickname"));
            }
            return nicknames;
        }catch (SQLException e){
            e.printStackTrace();
        }
        return null;
    }

     */ // 查询所有昵称(自己写的)
    public List<User> selectAll() {
        try (Connection conn = dbUtil.getConnection()) {
            String sql = "SELECT id,username,password,nickname,age " +
                    "FROM user ";
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery(sql);
            List<User> users = new ArrayList<>();
            while (rs.next()) {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                user.setAge(rs.getInt("age"));
                users.add(user);
            }
            return users;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }// 查询所有用户信息(老师写的)

    @Override
    public int update(User user) {
        try (Connection conn = dbUtil.getConnection()) {
            String sql = "UPDATE user " +
                    "SET password = ?,nickname = ?,age = ? " +
                    "WHERE username = ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, user.getPassword());
            ps.setString(2, user.getNickname());
            ps.setInt(3, user.getAge());
            ps.setString(4, user.getUsername());
            return ps.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }// 修改用户信息(自己写的)
}
